<template>
  <div>
    <el-dialog title="编辑角色" :visible="roleShow">
      <el-checkbox-group v-model="checkedCities">
        <el-checkbox v-for="city in cities" :key="city.id" :label="city.id">{{
          city.name
        }}</el-checkbox>
      </el-checkbox-group>
      <el-row type="flex" justify="center" style="margin-top: 20px">
        <el-button type="primary" @click="submitFn">确认</el-button> <el-button @click="removeFn">取消</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getrolesAPI } from '@/api/setting'

import { assignRolesAPI, getuserAPI } from '@/api/employees'
export default {
  props: {
    roleShow: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      checkedCities: [], // 选中的数组
      cities: [] // 要渲染的多选框数组
    }
  },
  created() {
    this.getrolesFn()
  },
  methods: {
    async getrolesFn() {
      const { rows } = await getrolesAPI({ page: 1 })
      this.cities = rows
    },
    async getuserFn(id) {
      const { roleIds } = await getuserAPI(id)
      this.checkedCities = roleIds
    },
    async submitFn() {
      const res = await assignRolesAPI({
        id: this.userId,
        roleIds: this.checkedCities
      })
      this.checkedCities = res.roleIds
      this.$message.success('角色权限分配成功')
      this.$bus.$emit('assignFn', false)
    },
    removeFn() {
      this.$bus.$emit('assignFn', false)
    }
  }
}
</script>

<style>
</style>
